<template>
  <div class="user-wrapper" :class="theme">
    <a-dropdown>
      <span class="action action-full ant-dropdown-link user-dropdown-menu">
        <a-avatar class="avatar" size="small" :src="getAvatar()" />
        <span v-if="isDesktop()"
          class="user-name">欢迎您，{{ `${userInfo().username} ` }}。 角色：{{`${(roleLists[0].label)}`}}</span>
      </span>
      <a-menu slot="overlay" class="user-dropdown-menu-wrapper">
        <a-menu-item key="2" @click="toChangePassword(userInfo().id)">
          <a-icon type="link" />
          <span>修改密码</span>
        </a-menu-item>
        <a-menu-item key="3" @click="handleLogout">
          <a-icon type="logout" />
          <span>退出登录</span>
        </a-menu-item>
      </a-menu>
    </a-dropdown>
    <user-password ref="userPassword"></user-password>
  </div>
</template>

<script>
// import SettingDrawer from '@/components/setting/SettingDrawer'
import { mapActions, mapGetters, mapState } from 'vuex'
import { mixinDevice } from '@/utils/mixin.js'
import UserPassword from '@/components/tools/UserPassword'
export default {
  name: 'UserMenu',
  mixins: [mixinDevice],
  components: {UserPassword},
  data () {
    return {
      roleLists: [],
      searchMenuOptions: [],
      searchMenuComp: 'span',
      searchMenuVisible: false
    }
  },
  props: {
    theme: {
      type: String,
      required: false,
      default: 'dark'
    }
  },
  created () {
    let lists = []
    this.searchMenus(lists, this.permissionMenuList)
    this.searchMenuOptions = [...lists]
    this.getRoleList()
  },
  computed: {
    ...mapState({
      // 后台菜单
      permissionMenuList: state => state.user.menuList

    })
  },
  watch: {
    device: {
      immediate: true,
      handler () {
        this.searchMenuVisible = false
        this.searchMenuComp = this.isMobile() ? 'a-modal' : 'span'
      }
    }
  },
  methods: {
    // 获取角色列表
    getRoleList () {
      this.roleLists = this.roleList().map((role) => {
        console.log('role', role)
        return {
          value: role.id,
          label: role.roleName
        }
      })
      console.log(this.roleLists)
    },
    ...mapActions(['Logout']),
    ...mapGetters(['avatar', 'userInfo', 'roleList']),
    getAvatar () {
      return '/static/logo.ico'
    },
    handleLogout () {
      const that = this
      this.$confirm({
        title: '提示',
        content: '真的要退出登录吗 ?',
        onOk () {
          return that.Logout({}).then(() => {
            window.location.href = '/'
            // window.location.reload()
          }).catch(err => {
            that.$message.error({
              title: '错误',
              description: err.message
            })
          })
        },
        onCancel () {
        }
      })
    },
    searchMenus (arr, menus) {
      for (let i of menus) {
        if (!i.hidden && i.type === '2') {
          arr.push(i)
        }
        if (i.children && i.children.length > 0) {
          this.searchMenus(arr, i.children)
        }
      }
    },
    filterOption (input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    toChangePassword(id) {
      this.$refs.userPassword.show(id)
    },
  }
}
</script>

<style lang="less" scoped>
.user-wrapper {
  font-size: 18px;
  .search-input {
    width: 180px;
    color: inherit;

    /deep/ .ant-select-selection {
      background-color: inherit;
      border: 0;
      border-bottom: 1px solid white;
      &__placeholder,
      &__field__placeholder {
        color: inherit;
      }
    }
  }
  .user-name {
    font-size: 16px;
    color: @placeholder-color;
  }
}
</style>

<style scoped>
.logout_title {
  color: inherit;
  text-decoration: none;
}
</style>
